<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta content="" name="description" />
    <meta content="webthemez" name="author" />
    <title>国内差旅申请表</title>
    <!-- Bootstrap Styles-->
    <link href="../assets/css/bootstrap.css" rel="stylesheet" />
    <!-- FontAwesome Styles-->
    <link href="../assets/css/font-awesome.css" rel="stylesheet" />

    <link href="../assets/css/select2.min.css" rel="stylesheet" >
    <link href="../assets/css/checkbox3.min.css" rel="stylesheet" >
    <!-- Custom Styles-->
    <!-- <link href="assets/css/custom-styles.css" rel="stylesheet" />-->
    <!-- Google Fonts-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="../js/layui/css/layui.css">
    <script src="../js/layui/layui.js"></script>
	<link rel="stylesheet" href="../css/img.css">
	
</head>
<body>


<!--/. NAV TOP  -->

<div id="page-wrapper" >
    <br /><br /><br />

    <!------>
    <div style="margin-left:-3%;">
    <div class="panel-body">
        <form class="form-inline">
            <div class="form-group" style="margin-left: 3%;">
                <label for="exampleInputName2">选择接收人角色:</label>
                <select class="selectbox" id="roleId"><!-- 这里是第一个下拉框的位置   角色 -->
                 <option value="">请选择</option>
                	<c:forEach items="${roleNameList }" var="role">
	                	<option value="${role.roleId}">${role.roleName }</option>
	                </c:forEach>
	        	</select>
            </div>
           <div class="form-group" style="margin-left: 1%;">
                <label for="exampleInputName2">选择接收人:</label>
                <select class="selectbox" id="userId"><!-- 这里是第二个下拉框的位置  用户 -->
	                <option value="">请选择</option>
	                <!-- <option value="HI">Hawaii</option>
	                <option value="IL">Illinois</option>
	                <option value="IA">Iowa</option>
	                <option value="KS">Kansas</option>
	                <option value="KY">Kentucky</option> -->
	        	</select>
            </div>
        <a href="#" class="btn btn-success" id="applyInsert">申请</a>
        <a href="${pageContext.request.contextPath}/chooseApplication/chooseType.do" class="btn btn-info">返回</a>
        <a href="#" class="btn btn-warning example">查看示例</a>
        </form>
    </div>
    </div>
    <div class="header">
        <h1 class="page-header" style="text-align: center">
          国内差旅 <small>（员工个人）</small>
        </h1>

    </div>

    <!-- <div id="page-inner"> -->
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">基本信息</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">
                            <div class="panel panel-default">
                               
                                <div class="panel-body">
                                    <div class="table-responsive">
                                        <table class="table">
                                        <c:forEach items="${basicList }" var="basic">

                                            <tr>
                                                <td style="text-align: right;">
                                             	       报账人：
                                                </td>
                                                <td>
                                                   ${basic.userName }
                                                </td>

                                                <td style="text-align: right;">
                                           		         报账单位名称：
                                                </td>
                                                <td id="userDept">
                                                   ${basic.departmentName }
                                                </td>
                                                <td style="text-align: right;">
                                              		  报账人电话：
                                                </td>
                                                <td>
                                                    ${basic.phone }
                                                </td>
                                            </tr>
                                            
                                            <tr>

                                                <td style="text-align: right;">
                                                    	公司代码：
                                                </td>
                                                <td>
                                                    ${basic.companyId }
                                                </td>
                                               
											<td style="text-align: right;">
                                                   	 预算指标：
                                                </td>
                                                <td>
                                                   	差旅费
													
                                                </td>
                                                 <td style="text-align: right;">发票类型：</td>
                                                <td>
                                                    <div class="form-group" style="margin-left: 3%;">
														<select class="selectbox" id="invoiceTypeId">
														<c:forEach items="${invoiceTypeList }" var="invoiceType"> 
															<option value="${invoiceType.invoiceTypeId}">${invoiceType.invoiceTypeName }</option>
														
														</c:forEach>
														</select>
													</div>
                                                </td>
                                            </tr>
                                            <tr>
											    <td style="text-align: right;">
                                                  	  报账单号：
                                                </td>
                                                <td>
                                                    <input type="text"  onfocus="this.blur();" class="form-control input-sm" aria-controls="dataTables-example" id="makeId" value="">
                                                </td>  

                                                <td style="text-align: right;">
                                                    	报账时间：
                                                </td>
                                                <td>
                                                    <input type="date" class="form-control inputAll" id="billTime">
                                                </td>
                                               <td style="text-align: right;">
                                                   	 收支方式：
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control input-sm inputAll"  id="payments" onblur="payment(this)">
                                                </td>
                                            </tr>
                                            <tr >
                                                <td style="text-align: right;">
                                                   	 附单据张数：
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control input-sm inputAll" aria-controls="dataTables-example" id="rebursementNumber" onblur="number(this)">
                                                </td>
                                                <td style="text-align: right;">
                                                   	 报账说明：
                                                </td>
                                                <td colspan="5">
                                                    <input type="search" class="form-control input-sm inputAll" aria-controls="dataTables-example" id="billExplain">
                                                </td>
                                                 
                                            </tr>
                                            </c:forEach>
                                        </table>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <div class="panel-body">

                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">差旅明细</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">
                            <div class="panel panel-default">
                                <!--<div class="panel-heading">
                                    Kitchen Sink
                                </div>-->
                                <div class="panel-body">
                                    <div class="table-responsive">
                                    	<!--这里有个增加行按钮-->
                                    	<button class="btn btn-primary" id="addMsg">添加行</button>
                                        <table class="table table-striped table-bordered table-hover" id="addTable">
                                            <thead>
                                            <tr>
                                                <th>出差开始日期</th>
												<th>出差结束日期</th>
                                                <th>出发地</th>
												<th>目的地</th>
                                                <th>交通工具</th>
                                                <th>天数</th>
                                                <th>伙食补贴费</th>
                                                <th>城市间交通费</th>
                                                <th>市内交通费</th>
                                                <th>住宿费</th>
                                                <th>进项税</th>
                                                <th>其他费用</th>
                                                <th>金额总计</th>
                                                <th>预算责任中心</th>
                                                <th>记账成本中心</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr id="addTr0" class="detailsTr">
                                                <td style="text-align: right;" class="detailsTd">
                                                    <input type="date" class="form-control inputAll startTime"  id="detailsTd">
                                                </td>
                                                <td>
                                                    <input type="date" class="form-control inputAll endTime" onblur="addDay(this)">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll day" onfocus="this.blur();">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll foodCost">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll cityExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll provinceExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll hotelExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll inputTax">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll otherCost" onblur="salary(this)">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll totalAmount" onfocus="this.blur();">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll accounting">
                                                </td>
                                            </tr>
                                            <tr id="addTr1" class="detailsTr">
                                              <td style="text-align: right;" class="detailsTd">
                                                    <input type="date" class="form-control inputAll startTime">
                                                </td>
                                                <td>
                                                    <input type="date" class="form-control inputAll endTime" onblur="addDay(this)">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td style="text-align: right;">
                                                    <input type="text" class="form-control inputAll day" onfocus="this.blur();">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll foodCost">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll cityExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll provinceExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll hotelExpense">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll inputTax">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll otherCost" onblur="salary(this)">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll totalAmount" onfocus="this.blur();">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll">
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control inputAll accounting">
                                                </td>
                                            </tr>
 
                                            <tr>
                                                <td style="text-align: center" colspan="5">小计</td>
                                                <td style="text-align: center;color: sandybrown" id="totalDay">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalFoodCost">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalCityCost">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalProvinceCost">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalHotelCost">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalInputTax">0</td>
                                                <td style="text-align: center;color: sandybrown" id="totalOtherCost">0</td>
                                                <td style="text-align: center;color: sandybrown"></td>
                                                <td style="text-align: center;color: sandybrown"></td>
                                                <td style="text-align: center;color: sandybrown"></td>
                                            </tr>

                                            <tr>
                                                <td style="text-align: center" colspan="4">含税合计金额（大写）</td>
                                                <td style="text-align: center;color: sandybrown" colspan="4" id="billSalaryOfTaxC">零元整</td>
                                                <td style="text-align: center" colspan="4">含税合计金额</td>
                                                <td style="text-align: center;color: sandybrown" colspan="4" id="billSalaryOfTax">0</td>
                                            </tr>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
                <div class="panel-body">

                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">其他</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="card-title">
                        <div class="title">
                            <!--选项卡-->
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    <ul class="nav nav-tabs">
                                        <li class=""><a href="#home" data-toggle="tab" aria-expanded="false">内部人信息</a>
                                        </li>
                                        <li class="active"><a href="#profile" data-toggle="tab" aria-expanded="true">报账单影像</a>

                                    </ul>

                                    <div class="tab-content">
                                        <div class="tab-pane fade" id="home">


                                            <!--表格-->
                                            <div class="panel panel-default">
                                                <!--   <div class="panel-heading">
                                                       Bordered Table
                                                   </div>-->

                                                <!-- /.panel-heading -->
                                                <div class="panel-body">
                                                    <div class="table-responsive table-bordered">
                                                        <table class="table">
                                                            <thead>
                                                            <tr>
                                                                <th>序号</th>
                                                                <th>收款人</th>
                                                                <th>所属部门</th>
                                                                <th>员工编号</th>
                                                                <th>收款类型</th>
                                                                <th>公司名称</th>
                                                                <th>开户行</th>
                                                                <th>银行账号</th>
                                                                <th>金额</th>
                                                            </tr>
                                                            </thead>
                                                            <tbody>
                                                            <tr>
                                                                <td>1</td>
                                                               
                                                                <td>
                                                                    ${otherMessage.userName}
                                                                </td>

                                                                <td>
																	${otherMessage.departmentName }
                                                                </td>
                                                                <td>
                                                                    ${otherMessage.userId }
                                                                </td>
                                                                <td>
                                                                	${otherMessage.receivablesType }
                                                                </td>
                                                                <td>
																	${otherMessage.companyName }
                                                                </td>
                                                                <td>
																	${otherMessage.bankName }
                                                                </td>
                                                                <td>
																	${otherMessage.bankNumber }

                                                                </td>
                                                                
                                                                <td>
                                                                    <input type="text" class="form-control inputAll" id="billSalary">
                                                                </td>

                                                            </tr>

                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                            </div>
      
                                        </div>
                                        <div class="tab-pane fade active in" id="profile">
                                            <div class="layui-upload">
                                                <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
                                                <div class="layui-upload-list">
                                                    <table class="layui-table">
                                                        <thead>
                                                        <th>文件名</th>
                                                        <th>大小</th>
                                                        <th>状态</th>
                                                        <th>操作</th>
                                                        </thead>
                                                        <tbody id="demoList"></tbody>
                                                    </table>
                                                </div>
												 <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
														预览图：
														<div class="layui-upload-list" id="demo2"></div>
													</blockquote>
                                                <button type="button" class="layui-btn" id="testListAction">开始上传</button>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>

</div>
<!-- /. PAGE INNER  -->
</div>
<!-- /. PAGE WRAPPER  -->

<!-- JS Scripts-->
<!-- jQuery Js -->
<script src="../assets/js/jquery-1.10.2.js"></script>
<!-- Bootstrap Js -->
<script src="../assets/js/bootstrap.min.js"></script>
<!-- Metis Menu Js -->
<script src="../assets/js/jquery.metisMenu.js"></script>
<script src="../assets/js/select2.full.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".selectbox").select2();
    });

    //查看示例
    layui.use('layer', function(){
        var layer = layui.layer;
    });
    $('.example').on('click', function(){
        layer.open({
            type: 2,
            title: '国内差旅示例',
            maxmin: true,
            shadeClose: false, //点击遮罩关闭层
            area : ['80%' , '80%'],
            content: ['${pageContext.request.contextPath}/chooseApplication/applyDemo.do?reimbursementName=domestictravelDemo','yes']
        });
    });
    
    /* $("#detailsTd").on("blur",function(){
    	console.log("!!!!"); */
    layui.use('upload', function(){
        var $ = layui.jquery
                ,upload = layui.upload;
        //多文件列表示例
        var demoListView = $('#demoList')
                ,uploadListIns = upload.render({
                    elem: '#testList'
                    ,url: '${pageContext.request.contextPath}/upload/uploadFile.do'
                    ,accept: 'file'
                    ,size: 51200 //最大允许上传文件大小  kb
                    ,multiple: true //是否支持多文件上传
                    ,auto: false //文件选择后是否自动上传
                    ,bindAction: '#testListAction'
                    ,before: function(input){
                    	var data= {"invoiceTime":$("#detailsTd").val(),
                        	"invoiceTypeId":$('#invoiceTypeId option:selected').val(),
                        	"reimbursementId":$("#makeId").val()};
                    	this.data = data;
                    }
                    ,choose: function(obj){
                        var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                        //读取本地文件
                        obj.preview(function(index, file, result){
                            var tr = $(['<tr id="upload-'+ index +'">'
                                ,'<td>'+ file.name +'</td>'
                                ,'<td>'+ (file.size/1024).toFixed(1) +'kb</td>'
                                ,'<td>等待上传</td>'
                                ,'<td>'
                                ,'<button class="layui-btn layui-btn-mini demo-reload layui-hide">重传</button>'
                                ,'<button class="layui-btn layui-btn-mini layui-btn-danger demo-delete">删除</button>'
                                ,'</td>'
                                ,'</tr>'].join(''));

                            //图片预览
                            $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="yulanimg" id="img-'+ index +'">')


                            //单个重传
                            tr.find('.demo-reload').on('click', function(){
                                obj.upload(index, file);
                            });

                            //删除
                            tr.find('.demo-delete').on('click', function(){
                                delete files[index]; //删除对应的文件
                                tr.remove();
                                console.log(index);
                                var str ='#img-'+index;
                                console.log(str);
                                $(str).remove()
                                uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                            });



                            demoListView.append(tr);
                        });
                    }
                    ,done: function(res, index, upload){
                        if(res.code == 0){ //上传成功
                            var tr = demoListView.find('tr#upload-'+ index)
                                    ,tds = tr.children();
                            tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                            tds.eq(3).html(''); //清空操作
                            return delete this.files[index]; //删除文件队列已经上传成功的文件
                        }
                        this.error(index, upload);
                    }
                    ,error: function(index, upload){
                        var tr = demoListView.find('tr#upload-'+ index)
                                ,tds = tr.children();
                        tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                        tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                    }
                });
			});
   /*  }); */




/* 这里需要点击角色名称 获取到当前角色id  然后通过ajax去后台找当前角色的用户  */

		$("#roleId").on("change",function(){
			var roleId = this.value;//当前的角色id
			console.log(roleId);
			console.log("11111");
			$.ajax({
				type:"post",
				url:"${pageContext.request.contextPath}/travelApplication/listUserNameAll.do",
				dataType:"json",
				data:{"roleId":roleId},
				success:function(data){
					console.log("成功了");
					if(data == null || data == ""){
						console.log("没数据");
						$("#userId").children().remove();
					}else {
						var str = "";
						$("#userId").children().remove();
						$.each(data,function(key,val){
							str += '<option value='+ val.userId +'>'+val.userName+'</option>';
							console.log(key);
							console.log("-----");
							console.log(val);
						});
						$("#userId").append(str);
					}
				},
				error:function(data){
					console.log(data);
				}
			});
		});
		
		//生成唯一的报销id
		function getId(){
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth();
			var day = date.getDate();
			var hour = date.getHours();
			var minute = date.getMinutes();
			var second = date.getSeconds(); 
			var id = "";
			id = year.toString()+month.toString()+day.toString()+hour.toString()+minute.toString()+second.toString();
			return id;
		}
		 
		//在报销单号处显示报销id
		//进入界面就生成了唯一的报销id
		window.onload = function makeId(){
			
			var getid = getId();
			var reId = document.getElementById("makeId");//得到位置
			reId.value = getid;
			
		}
		
		//选择发送人，显示记账成本中心，根据发送人id，查找部门名
		var userIdDept = document.getElementById("userId");
		userIdDept.onchange=function(){
		    var userId = userIdDept.options[userIdDept.selectedIndex].value;
			$.ajax({
				type:"post",
				url:"${pageContext.request.contextPath}/staffFormApplication/getDeptName.do",
				dataType:"json",
				data:{"userId":userId},
				success:function(data){
					$(".accounting").val(data.deptName);
				},
			});
	    }


   /* 添加行	 */
   $(function(){
	   //点击添加行
	   $("#addMsg").click(function(){
		   var trId = $(".detailsTr").length;
		   var addtr = "<tr id='addTr"+trId+"' class='detailsTr'>"+
				           "<td style='text-align: right;' class='detailsTd'><input type='date' class='form-control startTime'></td>"+
				           "<td> <input type='date' class='form-control endTime' onblur='addDay(this)'></td>"+
				           "<td style='text-align: right;'><input type='text' class='form-control'></td>"+
				           "<td style='text-align: right;'><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td style='text-align: right;'><input type='text' class='form-control day' onfocus='this.blur();'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control' onblur='salary(this)'></td>"+
				           "<td><input type='text' class='form-control' onfocus='this.blur();'></td>"+
				           "<td><input type='text' class='form-control'></td>"+
				           "<td><input type='text' class='form-control accounting'></td>"+
				         "</tr>";
		   $(addtr).insertAfter($("#addTr1"));	   
	   });
   });

	layui.use('layer', function(){
	    var layer = layui.layer;
	});
   
	/* 点击出差结束日期计算天数 */
   function addDay(endTime){
	   var $endTime = $(endTime);
	   var preDay = $endTime.parents("td").next().next().next().next().find("input").val();
	   var totalDay = $("#totalDay").text();
	   //计算总天数
	   if(preDay != ""){
		   totalDay = parseInt(totalDay) - parseInt(preDay);
	   }
		
	   //出差开始日期
	   var startTime = $endTime.parents("td").prev().find("input").val();
	   var startNum = parseInt(startTime.replace(/-/g, ''), 10);
	   //出差结束日期
	   var endTime = $endTime.val();
	   var endNum = parseInt(endTime.replace(/-/g, ''), 10);
	   
	   var day = endNum - startNum; 
	   
	   if(startTime == "" || endTime == ""){
		   layer.msg("结束时间和开始时间不能为空！");
		   //$endTime.parents("td").next().next().next().next().find("input").val("0");
	   }else if(endNum < startNum){
		   layer.msg("结束时间不能在开始时间之前！");
		   $endTime.parents("td").prev().find("input").val("");
		   //$endTime.parents("td").next().next().next().next().find("input").val("");
		   $endTime.val("");
	       return false;
	   }else if(day == 0){
		   $endTime.parents("td").next().next().next().next().find("input").val("1");
	   }else if(day > 0){
		   $endTime.parents("td").next().next().next().next().find("input").val(DateDiff(startTime, endTime));
	   } 
	   
	   //计算总天数
	   var addDay = $endTime.parents("td").next().next().next().next().find("input").val();
	   totalDay = parseInt(totalDay) + parseInt(addDay);
	   $("#totalDay").text(totalDay);
	   
	   //计算天数
	   function DateDiff(sDate1, sDate2) {  //sDate1和sDate2是yyyy-MM-dd格式 
		    var aDate, oDate1, oDate2, iDays;
		    aDate = sDate1.split("-");
		    oDate1 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);  //转换为yyyy-MM-dd格式
		    aDate = sDate2.split("-");
		    oDate2 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);
		    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
		    return iDays;  //返回相差天数

		}
   }
	
	/* 点击其他费用结束计算总金额 */
	function salary(otherCost){
		var $otherCost = $(otherCost);
		//伙食补贴费
		var foodCost = $otherCost.parents("td").prev().prev().prev().prev().prev().find("input").val();
		//城市间交通费
		var cityExpense = $otherCost.parents("td").prev().prev().prev().prev().find("input").val();
		//市内交通费
		var provinceExpense = $otherCost.parents("td").prev().prev().prev().find("input").val();
		//住宿费
		var hotelExpense = $otherCost.parents("td").prev().prev().find("input").val();
		//进项税
		var inputTax = $otherCost.parents("td").prev().find("input").val();
		//其他费用
		var otherCost = $otherCost.val();
        //天数
        var day = $otherCost.parents("td").prev().prev().prev().prev().prev().prev().find("input").val();
		//用户ID
		var userId = 19001;
		//预算责任中心
		var userDept = $.trim($("#userDept").text());
		$otherCost.parents("td").next().next().find("input").val(userDept);	
		
		//总伙食补贴费
		var totalFoodCost = $("#totalFoodCost").text();
		//总城市间交通费
		var totalCityCost = $("#totalCityCost").text();
		//市内交通费
		var totalProvinceCost = $("#totalProvinceCost").text();
		//住宿费
		var totalHotelCost = $("#totalHotelCost").text();
		//进项税
		var totalInputTax = $("#totalInputTax").text();
		//其他费用
		var totalOtherCost = $("#totalOtherCost").text();
		//总金额
		var totalSalary = $otherCost.parents("td").next().find("input").val();
		//全部总金额
		var totalSalaryAll = $("#billSalaryOfTax").text();
		
		console.log(totalSalary);
		//移除已有的金额，重新增加金额
		if(totalSalary != ""){				
			totalFoodCost = parseInt(totalFoodCost) - parseInt(foodCost);			
			totalCityCost = parseInt(totalCityCost) - parseInt(cityExpense);			
			totalProvinceCost = parseInt(totalProvinceCost) - parseInt(provinceExpense);			
			totalHotelCost = parseInt(totalHotelCost) - parseInt(hotelExpense);		
			totalInputTax = parseInt(totalInputTax) - parseInt(inputTax);		
			totalOtherCost = parseInt(totalOtherCost) - parseInt(otherCost);
			totalSalaryAll = parseInt(totalSalaryAll) - parseInt(totalSalary);
		}
		
		if(foodCost=="" || provinceExpense=="" || cityExpense=="" || hotelExpense=="" || otherCost=="" || inputTax=="" || day==""){
			layer.msg("日期和费用不能为空！");
			return false;
		}else{
			//根据用户id查询该用户的报销上限
			$.ajax({
		    	url:"${pageContext.request.contextPath}/travelApplication/getFoodSalary.do",
		    	type:"POST",
		    	dataType:"json",
		    	async:true,
		        data:{"userId":userId},
		        success:function(data){
		        	$.each(data,function(key,val){
		        		if(parseFloat(foodCost)<=val.伙食补贴费  && parseFloat(provinceExpense)<=val.市内交通费 && parseFloat(cityExpense)<=val.城市间交通费 && parseFloat(hotelExpense)<=val.住宿费 && parseFloat(otherCost)<=val.其他费用){
		        			//总金额(不含税)
		        			var total =  parseFloat(foodCost*day) + parseFloat(provinceExpense) + parseFloat(cityExpense) + parseFloat(hotelExpense) + parseFloat(otherCost);
		        			//总金额(含税)
		        			var totalofTax =  parseFloat(foodCost*day) + parseFloat(provinceExpense) + parseFloat(cityExpense) + parseFloat(hotelExpense) + parseFloat(otherCost) + parseFloat(inputTax);
		        			console.log(total+" "+totalofTax);
		        			$otherCost.parents("td").next().find("input").val(totalofTax);
		        			
		        			//总伙食补贴费
		        			totalFoodCost = parseInt(totalFoodCost) + parseInt(foodCost);
		        			$("#totalFoodCost").text(totalFoodCost);
		        			//总城市间交通费
		        			totalCityCost = parseInt(totalCityCost) + parseInt(cityExpense);
		        			$("#totalCityCost").text(totalCityCost);
		        			//市内交通费
		        			totalProvinceCost = parseInt(totalProvinceCost) + parseInt(provinceExpense);
		        			$("#totalProvinceCost").text(totalProvinceCost);
		        			//住宿费
		        			totalHotelCost = parseInt(totalHotelCost) + parseInt(hotelExpense);
		        			$("#totalHotelCost").text(totalHotelCost);
		        			//进项税
		        			totalInputTax = parseInt(totalInputTax) + parseInt(inputTax);
		        			$("#totalInputTax").text(totalInputTax);
		        			//其他费用
		        			totalOtherCost = parseInt(totalOtherCost) + parseInt(otherCost);
		        			$("#totalOtherCost").text(totalOtherCost);
		        			//全部总金额
		        			totalSalaryAll = parseInt(totalSalaryAll) + parseInt(totalofTax);
		        			$("#billSalaryOfTax").text(totalSalaryAll);
		        			$("#billSalaryOfTaxC").text(numtochinese(totalSalaryAll));
		        			
		        			function numtochinese(Num)
		        			{ 
		        			   var part = String(Num);		   	
		        			   var newchar = "";    
		                       for(var i=part.length-1;i>=0;i--) { 
		        			        var tmpnewchar = "" ;
		        			        var perchar = part[i]; 
		        			        switch(perchar){ 
		        			        case "0": tmpnewchar="零" + tmpnewchar ;break; 
		        			        case "1": tmpnewchar="壹" + tmpnewchar ;break; 
		        			        case "2": tmpnewchar="贰" + tmpnewchar ;break; 
		        			        case "3": tmpnewchar="叁" + tmpnewchar ;break; 
		        			        case "4": tmpnewchar="肆" + tmpnewchar ;break; 
		        			        case "5": tmpnewchar="伍" + tmpnewchar ;break; 
		        			        case "6": tmpnewchar="陆" + tmpnewchar ;break; 
		        			        case "7": tmpnewchar="柒" + tmpnewchar ;break; 
		        			        case "8": tmpnewchar="捌" + tmpnewchar ;break; 
		        			        case "9": tmpnewchar="玖" + tmpnewchar ;break; 
		        			            } 
		        			        switch(part.length-i-1){ 
		        			        case 0: tmpnewchar = tmpnewchar +"元" ;break; 
		        			        case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break; 
		        			        case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break; 
		        			        case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;    
		        			        case 4: tmpnewchar= tmpnewchar +"万" ;break; 
		        			        case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break; 
		        			        case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break; 
		        			        case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break; 
		        			        case 8: tmpnewchar= tmpnewchar +"亿" ;break; 
		        			        case 9: tmpnewchar= tmpnewchar +"拾" ;break; 
		        			            } 
		        			        newchar = tmpnewchar + newchar; 	        			        
		        			    } 
		        			    //替换所有无用汉字 
		        			    while(newchar.search("零零") != -1) 
		        			           newchar = newchar.replace("零零", "零"); 
		        			           newchar = newchar.replace("零亿", "亿"); 
		        			           newchar = newchar.replace("亿万", "亿"); 
		        			           newchar = newchar.replace("零万", "万");    
		        			           newchar = newchar.replace("零元", "元"); 
		        			           newchar = newchar.replace("零角", ""); 
		        			           newchar = newchar.replace("零分", ""); 
		        			       
		        			           if(newchar.charAt(newchar.length-1) == "元"){
			        			            newchar = newchar+"整";
				        			           return newchar; 
		        			           } 
		        			   }
		        			       			
		        			return false;
		        		}else{
		        			layer.msg("费用超过限额！");
		        			$otherCost.parents("td").next().find("input").val("");
		        			return false;
		        		}
					});
		        },
		    });
			
		}		
	}
	
/* 点击申请按钮  */
    $(function(){
    	
    	//界面刷新，输入框为空
    	$(".inputAll").val("");
    	
    	//点击申请按钮
    	 $("#applyInsert").click(function(){
        	 //得到用户ID
        	 var userId = 19001;
        	 
        	 //得到收支方式
        	 var payments = $("#income").val();
        	 
        	 //得到报销ID
        	 var reimbursementId = $("#makeId").val();
        	 
        	 //得到报销时间
        	 var reimbursementTime = $("#billTime").val();
        	 
        	 //得到报销说明
        	 var reimbursementExplain = $("#billExplain").val();
        	 
        	//得到附单据张数
        	 var reimbursementNumber = $("#billNum").val();
        	 
        	//得到报账金额（含税）
        	 var reimbursementMoneyOfTax = $("#billSalaryOfTax").text();
        	 
        	//得到报账金额（不含税）
        	 var reimbursementMoney = $("#billSalary").val();
        	 
        	 //接受者ID
        	 var accepterId = $('#userId option:selected').val();
        	 
        /* 	 //得到发票类型ID
        	 var invoiceTypeId = $('#invoiceTypeId option:selected').val();
        	 console.log(invoiceTypeId); */
        	 
        	 if(userId !="" && payments !="" && reimbursementId !="" && reimbursementTime !="" && reimbursementExplain !="" && reimbursementNumber !=""
        		 && reimbursementMoney !="" && reimbursementMoneyOfTax !="" && accepterId !=""){
        		 
        			var jsonMsg = {
        					"userId":userId,
        					"payments":payments,
        					"reimbursementId":reimbursementId,
        					"reimbursementTime":reimbursementTime,
        					"reimbursementExplain":reimbursementExplain,
        					"reimbursementNumber":reimbursementNumber,
        					"reimbursementMoney":reimbursementMoney,
        					"reimbursementMoneyOfTax":reimbursementMoneyOfTax,
        					"accepterId":accepterId
        				}
        			
        			$.ajax({
    					type:"post",
    					url:"${pageContext.request.contextPath}/travelApplication/insertTravelMsg.do",
    					dataType:"json",
    					data:jsonMsg,
    					contentType: "application/x-www-form-urlencoded",
    					success:function(data){
    						console.log(data);
    						if(data != 0){
    							layer.msg("新增成功！");
    						}else{
    							layer.msg("新增失败！");
    						}
    					},
    					error:function(){
    						console.log("我错了");
    					}
    				});
        	 }
        	 
        	 //差旅明细
        	 for(var t=0;t<$(".detailsTr").length;t++){
        		 //出差开始日期
        		 var businessTravelStartDate = $("#addTr"+t).children("td").find("input").val();
        		 console.log(businessTravelStartDate);
        		 //出差结束日期
        		 var businessTravelEndDate = $("#addTr"+t).children("td").next().find("input").val();
        		 console.log(businessTravelEndDate);
        		 //出发地
        		 var businessTravelStartSection = $("#addTr"+t).children("td").next().next().find("input").val();
        		 console.log(businessTravelStartSection);
        		 //目的地
        		 var businessTravelEndSection = $("#addTr"+t).children("td").next().next().next().find("input").val();
        		 console.log(businessTravelEndSection);
        		 //交通工具
        		 var vehicleName = $("#addTr"+t).children("td").next().next().next().next().find("input").val();
        		 console.log(vehicleName);
        		 //伙食补贴费
        		 var foodCost = $("#addTr"+t).children("td").next().next().next().next().next().next().find("input").val();
        		 console.log(foodCost);
        		 //城市间交通费
        		 var cityTrafficExpense = $("#addTr"+t).children("td").next().next().next().next().next().next().next().find("input").val();
        		 console.log(cityTrafficExpense);
        		 //市内交通费
        		 var provinceTrafficExpense = $("#addTr"+t).children("td").next().next().next().next().next().next().next().next().find("input").val();
        		 console.log(provinceTrafficExpense);
        		 //住宿费
        		 var hotelExpense = $("#addTr"+t).children("td").next().next().next().next().next().next().next().next().next().find("input").val();
        		 console.log(hotelExpense);
        		 //进项税
        		 var inputTax = $("#addTr"+t).children("td").next().next().next().next().next().next().next().next().next().next().find("input").val();
        		 console.log(inputTax);
        		 //其他费用
        		 var otherCost = $("#addTr"+t).children("td").next().next().next().next().next().next().next().next().next().next().next().find("input").val();
        		 console.log(otherCost);
        		 if(userId != "" && reimbursementId != "" && businessTravelStartDate != "" && businessTravelEndDate != "" && businessTravelStartSection != "" && + 
        		    businessTravelEndSection != "" && vehicleName != "" && foodCost != "" && cityTrafficExpense != "" && provinceTrafficExpense != "" && hotelExpense != "" && inputTax != "" && otherCost != ""){
        			 var jsonDetails = {
             				"userId":userId,
          					"reimbursementId":reimbursementId,
          					"businessTravelStartDate":businessTravelStartDate,
          					"businessTravelEndDate":businessTravelEndDate,
          					"businessTravelStartSection":businessTravelStartSection,
          					"businessTravelEndSection":businessTravelEndSection,
          					"vehicleName":vehicleName,
          					"foodCost":foodCost,
          					"cityTrafficExpense":cityTrafficExpense,
          					"provinceTrafficExpense":provinceTrafficExpense,
          					"hotelExpense":hotelExpense,
          					"inputTax":inputTax,
          					"otherCost":otherCost
             		 }
        			 $.ajax({
     					type:"post",
     					url:"${pageContext.request.contextPath}/travelApplication/insertTravelDetailsMsg.do",
     					dataType:"json",
     					data:jsonDetails,
     					contentType: "application/x-www-form-urlencoded",
     					success:function(data){
     						console.log(data);
     						if(data != 0){
     							layer.msg("新增成功！");
     						}else{
     							layer.msg("新增失败！");
     						}
     					},
     					error:function(){
     						console.log("我错了");
     					}
     				});
        		 }
        		
        	 }
        	 
        });
    	
    });
    
  //判断收支方式
	 function payment(payments) {
		 var $payments = $(payments);
		 var payments = $payments.val();
		 if(payments != "现金支付" && payments != "集中支付" && payments != "支付宝支付" && payments != "微信支付") {
			 layer.msg("请输入“现金支付”、“集中支付”、“支付宝支付”、“微信支付”中的一种");
			 $payments.val("");
		 }
	 }
	//判断附单据张数是否为数字
	  function number(reimbursementNumber) {
		 var $reimbursementNumber = $(reimbursementNumber);
		 var reimbursementNumber = $reimbursementNumber.val();
		 if(isNaN(reimbursementNumber) == true) {
			 layer.msg("请输入数字");
			 $reimbursementNumber.val("");
		 } else if(reimbursementNumber <= 0) {
			 layer.msg("请输入大于零的数");
			 $reimbursementNumber.val("");
		 } else if(parseInt(reimbursementNumber) != reimbursementNumber) {
			 layer.msg("请输入大于零的整数");
			 $reimbursementNumber.val("");
		 }
	 } 

</script>

</body>
</html>